package com.zsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.dto.SetmealDTO;
import com.zsc.dto.SetmealPageQueryDTO;
import com.zsc.entity.Category;
import com.zsc.entity.Setmeal;
import com.zsc.entity.SetmealDish;
import com.zsc.result.PageResult;
import com.zsc.service.SetmealService;
import com.zsc.mapper.SetmealMapper;
import com.zsc.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author cjr165123050
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2024-05-19 11:12:16
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithDish(SetmealDTO setmealDTO) {
        // 向套餐表写入数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        save(setmeal);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        updateById(setmeal);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealDTO) {
        Page<Setmeal> setmealPage = new Page<>(setmealDTO.getPage(), setmealDTO.getPageSize());
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(Setmeal::getName,setmealDTO.getName());
        Page<Setmeal> page = this.page(setmealPage, setmealLambdaQueryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        removeByIds(ids);
    }

    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal = getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        return setmealVO;
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        updateById(setmeal);
    }
}




